package com.ruifu.bom.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.ruifu.bom.dto.MaterielAndVersionsDTO;
import com.ruifu.bom.service.MaterielStructureService;
import com.ruifu.bom.service.MaterielVersionsService;
import com.ruifu.bom.vo.MaterielBom;
import com.ruifu.bom.vo.MaterielBomCondition;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/bomView")
@Api(tags = "产品零部件视图管理")
@Slf4j
public class BomMultiviewController {

    @Autowired
    MaterielStructureService materielStructureService;
    @Autowired
    MaterielVersionsService materielVersionsService;


    /**
     * 根据传入的节点id查询该产品下的所有下一级节点零部件信息
     *
     * @param pMvId 选中节点物料版本id
     * @param mvIds 对应子节点物料版本id集合
     * @return
     */
    @PostMapping("/examineParts/{pMvId}")
    @ApiOperation(value = "查询子节点零部件详情", notes = "传入参数为当前选中节点的下一级节点版本id数组")
    public R examineParts(@PathVariable("pMvId") Long pMvId, @RequestBody List<Long> mvIds) {

        return R.ok(materielStructureService.findByIds(pMvId, mvIds));
    }

    /**
     * 根据传入的根节点id，查询该节点下面所有的子节点基本信息
     *
     * @param mvId      物料版本id
     * @param condition 查询条件
     * @return
     */
    @PostMapping("/treeMap/{mvId}")
    @ApiOperation(value = "bom结构树信息查询", notes = "根据传入的根节点id，查询该节点下面所有的子节点基本信息")
    public R getTreeMap(@PathVariable("mvId") Long mvId, @RequestBody(required = false) MaterielBomCondition condition) {
        List<MaterielBom> bomList = new ArrayList<>();

        //如果没有传入查询条件，则初始化一个默认的查询条件
        if (null == condition) {
            condition = new MaterielBomCondition();
        }

        //递归查询
        materielStructureService.findMaterielByPid(mvId, condition, bomList);

        return R.ok(bomList);
    }

    /**
     * 根据节点id，删除下级关联所有子节点关联
     *
     * @param mlId
     * @return
     */
    @DeleteMapping("/removeParts/{pMvId}/{mlId}")
    @ApiOperation(value = "移除下级件", notes = "根据节点id，删除下级关联所有子节点关联")
    public R removeParts(@PathVariable("pMvId") Long pMvId, @PathVariable("mlId") Long mlId) {
        return materielStructureService.removeParts(pMvId, mlId);
    }

    /**
     * 根据节点物料版本id，查询当前物料节点的相关详细信息
     *
     * @param mvId
     * @return
     */
    @GetMapping("/partsDetail/{mvId}")
    @ApiOperation(value = "物料详情信息查看", notes = "根据节点物料版本id，查询当前物料节点的相关详细信息")
    public R partsDetail(@PathVariable("mvId") String mvId) {
        try {
            return materielVersionsService.findByMvId(mvId);
        } catch (Exception e) {
            log.error(e.getMessage());
            return R.failed(e.getMessage());
        }
    }

    /**
     * 查找添加下级件
     *
     * @param mlIds 用户勾选的物料id数组集合
     * @param mvId  用户选中的当前物料版本id
     * @return
     */
    @PostMapping("/addParts/{mvId}/{bomType}")
    @ApiOperation(value = "查找添加下级件", notes = "查找添加下级件")
    public R addBomStructureBatch(@RequestBody List<Long> mlIds, @PathVariable("mvId") Long mvId, @PathVariable("bomType") String bomType) {
        return materielStructureService.addBomStructureBatch(mlIds, mvId, bomType);
    }

    /**
     * 新增下级件
     *
     * @param materielAndVersionsDTO
     * @param mvId
     * @return
     */
    @PostMapping("/addPart/{mvId}/{bomType}")
    @ApiOperation(value = "新增下级件", notes = "新增物料并添加到当前选中节点之下")
    public R save(@RequestBody MaterielAndVersionsDTO materielAndVersionsDTO, @PathVariable("mvId") Long mvId, @PathVariable("bomType") String bomType) {
        //先进行物料版本和物料表数据的增加操作
        R result = materielVersionsService.saveMaterielAndVersion(materielAndVersionsDTO);

        if (result.ok()) {
            //再进行物料结构表的添加操作
            return materielStructureService.addBomStructure(materielAndVersionsDTO.getMaterielDTO().getMlId(), mvId, bomType);
        } else {
            return result;
        }
    }
}
